package au.com.introprogexercise.core.excercises;

/**
 * Created by olga on 30/05/14.
 */
public class DiamondExercise
{
    public static int getMaximumNumberOfAsterisksInDiamond( int diamondRows )
    {
        return 1 + 2 * (diamondRows - 1);
    }

    public static int getMiddleAsteriskPosition( int maxNumberOfAsterisks )
    {
        if( maxNumberOfAsterisks == 0 )
            return 0;
        else
            return (maxNumberOfAsterisks / 2) + 1;
    }

    private void printDiamondRow( int offset, int middleAsterisksPosition,int maxNumberOfAsteriksk )
    {
        for( int printPosition = 1; printPosition <= maxNumberOfAsteriksk; printPosition++ )
        {
            if (printPosition < (middleAsterisksPosition - offset) || printPosition > (middleAsterisksPosition + offset)) {
                System.out.print(" ");
            }
            else
            {
                System.out.print("*");
            }
        }
        System.out.println();
    }

    public void printIsoscelesTriangle( int numberOfRows )
    {
        int maxNumberOfAsteriksk = getMaximumNumberOfAsterisksInDiamond( numberOfRows );
        int offset = 0;
        int middleAsterisksPosition = getMiddleAsteriskPosition(maxNumberOfAsteriksk);

        for( int row = 1; row <= numberOfRows; row++ )
        {
            printDiamondRow(offset, middleAsterisksPosition, maxNumberOfAsteriksk);
            offset++;
        }
    }

    private void printDiamondName()
    {
        System.out.println("Olga");
    }

    private void printDiamond( int numberOfRows, boolean printName )
    {
        int maxNumberOfAsteriksk = getMaximumNumberOfAsterisksInDiamond( numberOfRows );
        int offset = 0;
        int middleAsterisksPosition = getMiddleAsteriskPosition(maxNumberOfAsteriksk);

        for( int row = -(numberOfRows - 1); row < numberOfRows; row++ )
        {
            if( row == 0 && printName )
                printDiamondName();
            else
                printDiamondRow(offset, middleAsterisksPosition, maxNumberOfAsteriksk);

            offset = (row >= 0)? offset - 1: offset + 1;
        }
    }

    public void printDiamond( int numberOfRows )
    {
        printDiamond(numberOfRows, false);
    }

    public void printDiamondWithName( int numberOfRows )
    {
        printDiamond(numberOfRows, true);
    }
}
